var app = new Vue({
    el: '#app',
    data: {
        host: host,
        username: '',
        // old_integral: '',
        // new_pwd: '',
        // new_cpwd: '',
        name:'',
        user:'',
        new_integral:'',
        error_pwd: false,
        error_name: false,
    },
    mounted: function () {
        let u_json = $cookies.get('username')   //使用第三方库获取cookie
        let u_str = JSON.parse(u_json)    //将数据进行json反序列化
        this.username = eval(u_str)     //将unicode编码转为中文
        this.get_person()
    },

    methods: {
        get_person: function () {
            var url = this.host + '/AdminIntegral/'
            // console.log(url)
            axios.get(url, {
                responseType: 'json',
                withCredentials: true,
            })
                .then(response => {
                    // console.log(response.data)
                    if (response.data.code == 400) {

                        //next=是如果未登录要跳转的页面
                        location.href = 'admin_login.html?next='+location.href;
                        return
                    }
                    this.user = response.data.username;
                    // this.user = 
                    // this.username = response.data.context.username;
                    // this.nick_name = response.data.context.nick_name;
                    // this.mobile = response.data.context.mobile;
                    // this.wallet_income = response.data.context.wallet_income;
                    // this.email = response.data.context.email;
                    // this.email_active = response.data.context.email_active;


                })

                .catch(error => {
                    this.set_email = false;
                    location.href = 'admin_login.html'
                    console.log(error.response)
                })
        },
        //退出
        logoutfunc: function () {
            var url = this.host + '/logout/'
            axios.delete(url, {
                responseType: 'json',
                withCredentials: true,
            })
                .then(response => {
                    console.log(response.data)
                    location.href = 'admin_login.html'

                })
                .catch(error => {
                    console.log(error.response)
                })
        },

        //校验数字
        check_pwd: function () {
            var re = /^[0-9]*$/;
            console.log(this.new_integral.length);
            if (re.test(this.new_integral)) {
                if(this.new_integral.length > 5){
                    
                    this.error_pwd = true;
                }
                else{
                    this.error_pwd = false;
                }
                
            } else {
                this.error_pwd = true;
            }
        },

        //校验是否填写用户
        check_name: function () {
            // var re = /^[0-9A-Za-z]{8,20}$/;
            if (this.name.length > 0) {
                this.error_name = false;
            } else {
                this.error_name = true;
            }
        },

        // check_cpwd:function () {
        //     if(this.new_pwd != this.new_cpwd){
        //         this.error_cpwd=true;
        //     }else {
        //         this.error_cpwd=false;
        //     }
        // },

        //提交
        change_password: function () {
            this.check_name();
            this.check_pwd();
            // this.check_cpwd();
            

            if (this.error_pwd == true || this.error_name == true) {
                window.event.returnValue = false;
                return
            }
            var kouling = prompt("请输入管理员密码:");
            var url = this.host + '/AdminIntegral/';
            axios.post(url,{
                // old_pwd: this.old_pwd,
                new_integral: this.new_integral,
                kouling:kouling,
                name:this.name
                // new_cpwd: this.new_cpwd,
            },{
                responseType: 'json',
                withCredentials: true,
            })
                .then(response =>{
                    if(response.data.code==0){
                        // location.href='login.html'
                        alert('积分添加成功')
                    }else {
                        alert(response.data.errmsg)
                    }

            })
                .catch(error =>{

            })

        },

    }
})